package com.pro.exam.web.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.pro.exam.common.controller.BaseController;
import com.pro.exam.common.utils.DataUtils;
import com.pro.exam.web.model.TransDict;
import com.pro.exam.web.service.DictTransService;

@Controller
@RequestMapping(value = "/dic")
public class DicController extends BaseController{
	
	@Autowired
	DictTransService dictTransService;
	@Resource(name="redisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	
	@RequestMapping("/list.html")
	public String index(Model m,HttpServletRequest request){
		m.addAttribute("words", DataUtils.ANALYZER_WORDS.values());
		return "web/dic";
	}
	@RequestMapping("/add.html")
	public String translate(Model m,HttpServletRequest request){
		String ch=request.getParameter("ch");
		if (ch == null || ch.isEmpty()) {
			m.addAttribute("words", DataUtils.ANALYZER_WORDS.values());
			return "web/dic";
		}
		String en=request.getParameter("en");
		String jp=request.getParameter("jp");
		HashMap<String,String> map=new HashMap<String,String>();
		ch = ch.toLowerCase();
		map.put("ch", ch);
		map.put("en", en);
		map.put("jp", jp);
		TransDict td = new TransDict();
		td.setCh(ch);
		Map<String, String> trans = new HashMap<String, String>();
		trans.put("en", en);
		trans.put("jp", jp);
		td.setTranslates(JSONObject.toJSONString(trans));
		
		dictTransService.add(td);
		
		redisTemplate.opsForHash().put(ch, "ch", ch);
		redisTemplate.opsForHash().put(ch, "en", en);
		redisTemplate.opsForHash().put(ch, "jp", jp);
		redisTemplate.opsForSet().add("trans", ch);
		/*
		DataUtils.ANALYZER_WORDS.put(ch, map);
		DataUtils.SWITCH_INITIAL_WORDS_LIB=true;*/
		Map<String, HashMap<String, String>> returnMap = new HashMap<String,HashMap<String,String>>();
		Set<String> keys=redisTemplate.opsForSet().members("trans");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			HashMap<String, String> transMap = new HashMap<String, String>();
			String key = (String) it.next();
			String ch1=(String)redisTemplate.opsForHash().get(key, "ch");
			String en1=(String)redisTemplate.opsForHash().get(key, "en");
			String jp1=(String)redisTemplate.opsForHash().get(key, "jp");
			transMap.put("ch", ch1);
			transMap.put("en", en1);
			transMap.put("jp", jp1);
			returnMap.put(key, transMap);
		}
		DataUtils.ANALYZER_WORDS = returnMap;
		
		m.addAttribute("words", DataUtils.ANALYZER_WORDS.values());
		return "web/dic";
	}
	@RequestMapping("/del.html")
	public String generate(Model m,HttpServletRequest request){
		String ch=request.getParameter("ch");
		DataUtils.ANALYZER_WORDS.remove(ch);
		redisTemplate.opsForSet().remove("trans", ch);
		redisTemplate.delete(ch);
		m.addAttribute("words", DataUtils.ANALYZER_WORDS.values());
		return "web/dic";
	}
}
